package com.example.my_bookkeeping;

import android.content.Context;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

import com.example.my_bookkeeping.data.manager.UserManager;
import com.example.my_bookkeeping.data.user.User;
import com.example.my_bookkeeping.ui.account.AccountFragment;

import androidx.viewpager2.widget.ViewPager2;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class AccountFragmentTest {

    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);

    private Context context;
    private UserManager userManager;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        userManager = UserManager.getInstance();
    }

    @Test
    public void testAccountSpinnerDisplay() {
        // 获取 MainActivity
        MainActivity activity = activityRule.getActivity();

        // 切换到 AccountFragment 所在的页面（索引为 3）
        activity.runOnUiThread(() -> {
            ViewPager2 viewPager2 = activity.findViewById(R.id.view_pager2_main);
            if (viewPager2 != null) {
                viewPager2.setCurrentItem(3);
            }
        });

        // 等待 Fragment 加载完成（可能需要更复杂的同步机制）
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 获取 AccountFragment
        // 尝试通过 ViewPager2 的当前 item 获取 Fragment
        // 这是一个可能的 workaround，不保证在所有情况下都有效
        AccountFragment fragment = (AccountFragment) activity.getSupportFragmentManager()
                .findFragmentByTag("f" + 3); // ViewPager2 可能会使用 "f" + position 作为 tag

        assertNotNull("AccountFragment should not be null", fragment);

        // 获取 Spinner 和账户信息卡片
        View rootView = fragment.getView();
        assertNotNull("Fragment view should not be null", rootView);
        Spinner spinnerAccount = rootView.findViewById(R.id.spinner_account);
        TextView tvAccountName = rootView.findViewById(R.id.tv_account_name);
        TextView tvAccountCategory = rootView.findViewById(R.id.tv_account_category);
        TextView tvAccountBalance = rootView.findViewById(R.id.tv_account_balance);

        // 验证 Spinner 是否显示账户名
        User firstUser = userManager.getAll().get(0);
        assertEquals("Spinner should display the first user's name", firstUser.getName(), ((User) spinnerAccount.getSelectedItem()).getName());

        // 验证账户信息卡片是否正确更新
        assertEquals("Account name should match", firstUser.getName(), tvAccountName.getText().toString());
        assertEquals("Account category should match", firstUser.getRemark(), tvAccountCategory.getText().toString());
        assertEquals("Account balance should match", String.valueOf(firstUser.getAmount()), tvAccountBalance.getText().toString());
    }
} 